package com.dmy.chat.server.netty;

import com.dmy.chat.server.netty.coder.WebsocketDecoder;
import com.dmy.chat.server.netty.handler.MyHttpHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;

/**
 * create by huahuadelei for 2020/1/4
 */
public class ChatChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel sc) throws Exception {
        ChannelPipeline pipeline = sc.pipeline();
        // http 解码器
        pipeline.addLast("code",new HttpServerCodec());
        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
        pipeline.addLast("websocket-decoder",new WebsocketDecoder());
        // 自定义拦截器
        pipeline.addLast(new MyHttpHandler());

    }
}
